import { dataQueryAxios } from '@/utils/request.ts'

export const creatTableQuery = (data: { release_item_id: string }) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets`,
    method: 'post',
    data
  })
}

export const getTopTables = () => {
  return dataQueryAxios({
    url: '/api/data-query/v1/queryable-datasets',
    method: 'get',
    params: {
      page: 1,
      page_size: 5
    }
  })
}

export const deleteTableQuery = (id: string) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}`,
    method: 'delete'
  })
}

export const importExcel = (id: string, data: FormData) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/datasets:upload`,
    method: 'post',
    data
  })
}

export const queryData = (
  id: string,
  data: {
    keywords: string | null
    filters: []
    ids: Array<string> | null
    page: number
    page_size: number
  }
) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/data-items/search/${id}`,
    method: 'post',
    data
  })
}

export const getIds = (
  id: string,
  params: {
    page: number
    page_size: number
  }
) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/datasets`,
    method: 'get',
    params
  })
}

export const getRelTables = (release_item_id: string) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/release-items/${release_item_id}/relations`,
    method: 'get',
    params: {
      page: 1,
      page_size: 99999
    }
  })
}

export const importOneId = (
  id: string,
  data: {
    data_id: string
  }
) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/datasets`,
    method: 'post',
    data
  })
}

export const deleteDataItem = (id: string, dataset_id: string) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/datasets/${dataset_id}`,
    method: 'delete'
  })
}

export const importAllFavorData = (id: string) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/datasets:import-favorites`,
    method: 'get'
  })
}

export const importFilterData = (
  id: string,
  data: {
    filter_id: string
  }
) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/datasets:import-filters`,
    method: 'post',
    data
  })
}

export const cleanTheTableData = (id: string) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/datasets:clean`,
    method: 'get'
  })
}
type exportStruct = {
  columns: Array<{ field: string; name: string }>
  filename: string
}
export const exportTableData = (id: string, data: exportStruct) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/datasets:export`,
    method: 'post',
    data
  })
}

export const queryRelDataTable = (
  id: string,
  data: {
    ids: Array<string>
    related_id: string
    page: number
    page_size: number
  }
) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/queryable-datasets/${id}/search-related`,
    method: 'post',
    data
  })
}

export const exportRelTableData = (
  id: string,
  data: exportStruct & {
    query: {
      keywords: null
      filters: []
      relation: null
      ids: Array<string>
      page: 1
      page_size: 10000
    }
  }
) => {
  return dataQueryAxios({
    url: `/api/data-query/v1/data-items/search/${id}/:export`,
    method: 'post',
    data
  })
}
